package com.xyy.jd_clund.service.impl.erp;

import com.kingdee.service.ApiClient;
import com.kingdee.service.ApiException;
import com.kingdee.service.data.api.*;
import com.kingdee.service.data.entity.*;
import com.xyy.jd_clund.dto.jd_erp.ProducrDetail;
import com.xyy.jd_clund.service.JdDataInfoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class JdDataInfoServiceImpl implements JdDataInfoService {
    /**
     * 获取客户信息
     *
     * @param customerCustomerListReq 查询参数
     * @return
     */
    @Override
    public CustomerListRes getCustomer(CustomerCustomerListReq customerCustomerListReq) {
        CustomerListRes customerListRes = new CustomerListRes();
        try {
            CustomerApi customerApi = new CustomerApi();
            customerListRes = customerApi.customerCustomerList(customerCustomerListReq);
        } catch (ApiException e) {
            throw new RuntimeException(e.getMessage());
        }
        return customerListRes;
    }

    /**
     * 添加客户信息
     *
     * @param customerSaveReq 客户信息
     * @return
     */
    @Override
    public SaveReply saveCustomer(CustomerSaveReq customerSaveReq) throws ApiException {

        CustomerApi customerApi = new CustomerApi();
        SaveReply saveReply = customerApi.customerCustomerSave(customerSaveReq);
        return saveReply;
    }


    /**
     * 订单保存
     *
     * @param orderSaveReq 订单信息
     * @return
     * @throws ApiException
     */
    @Override
    public SaveReply saveOrder(SalOrderSaveReq orderSaveReq) throws ApiException {
        SalOrderApi salOrderApi = new SalOrderApi();
        SaveReply saveReply = salOrderApi.salOrderSave(orderSaveReq);
        return saveReply;
    }


    /**
     * 查询商品信息
     *
     * @param params 查询条件
     * @return
     */
    @Override
    public MaterialListReply productInfo(MaterialMaterialListReq params) {

        MaterialListReply materialListReply = new MaterialListReply();
        try {

            MaterialApi materialApi = new MaterialApi();
            materialListReply = materialApi.materialMaterialList(params);
        } catch (ApiException e) {
            throw new RuntimeException(e.getMessage());
        }
        return materialListReply;
    }


    /**
     * 获取商品单位信息
     *
     * @param number
     * @return
     */
    @Override
    public ProducrDetail getUnitId(String number) {
        try {

            MaterialApi materialApi = new MaterialApi();
            MaterialDetailReply materialDetailReply = materialApi.materialMaterialDetail("", number);
            List<Auxentity> auxEntity = materialDetailReply.getAuxEntity();
            if (auxEntity.isEmpty()) {
                log.info("商品: {} 未查询到单位信息", number);
                return new ProducrDetail();
            }
            Auxentity auxentity = auxEntity.get(0);
            ProducrDetail producrDetail = new ProducrDetail();
            BeanUtils.copyProperties(auxentity, producrDetail);
            producrDetail.setBaseUnitId(materialDetailReply.getBaseUnitId());
            return producrDetail;
        } catch (ApiException e) {
            throw new RuntimeException(e.getMessage());
        }

    }

    /**
     * 获取库存信息
     *
     * @param invListReq 查询参数
     * @return
     */
    @Override
    public Map<String, List<InvListResRow>> getStock(InventoryInvListReq invListReq) {
        try {
            InventoryApi inventoryApi = new InventoryApi();
            InvListRes invListRes = inventoryApi.inventoryInvList(invListReq);
            List<InvListResRow> rows = invListRes.getRows();
            Map<String, List<InvListResRow>> stringListList = null;
            stringListList = rows.stream().collect(Collectors.groupingBy(InvListResRow::getMaterialNumber));
            return stringListList;
        } catch (ApiException e) {
            throw new RuntimeException(e.getMessage());
        }

    }

    /**
     * 获取审核后得销售单
     *
     * @param orderNo
     * @return
     */
    @Override
    public List<String> getOrderComp(List<String> orderNo) {

        try {
            SalOrderApi salOrderApi = new SalOrderApi();
            List<String> ids = new ArrayList<>();
            for (String s : orderNo) {
                SalOrderDetailReply orderDetail = salOrderApi.salOrderDetail("", s);
                if (orderDetail == null) {
                    log.info("当前pop订单 未查询到金蝶销售单  请检查金蝶是否有当前订单 订单号: {}", s);
                    return new ArrayList<>();
                }
                if ("C".equals(orderDetail.getBillStatus())) {
                    ids.add(s);
                }
            }
            return ids;
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }

    }

    /**
     * 出库明细
     *
     * @param list
     * @return
     */
    @Override
    public List<SalOutBoundDetailReply> getOrderDeliver(List<String> list) {
        try {
            SalOutBoundApi salOutBoundApi = new SalOutBoundApi();
            List<SalOutBoundDetailReply> sal = new ArrayList<>();
            for (String s : list) {
                //通过模糊搜索字段查询到金蝶订单号
                SalOutBoundListReq req = new SalOutBoundListReq();
                req.setSearch(s);
                SalOutBoundListReply salOutBoundListReply = salOutBoundApi.salOutBoundList(req);
                List<SalOutBoundListReplyRow> rows = salOutBoundListReply.getRows();
                for (SalOutBoundListReplyRow row : rows) {
                    //通过金蝶订单号开始查询
                    SalOutBoundDetailReply salOutBoundDetailReply = salOutBoundApi.salOutBoundDetail("", row.getBillNo());
                    salOutBoundDetailReply.setBillNo(s);
                    if ("C".equals(salOutBoundDetailReply.getBillStatus())) {
                        sal.add(salOutBoundDetailReply);
                    }
                }
            }
            return sal;
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }

    //根据客户编码  查询id
    @Override
    public String getCustomerIdByNumber(String number) {
        try {
            CustomerApi customerApi = new CustomerApi();
            CustomerDetailRes customerDetailRes = customerApi.customerCustomerDetail("", number, false, false, false, false);
            return customerDetailRes.getId();
        } catch (ApiException e) {
            throw new RuntimeException(e);
        }
    }
}
